// 自定义 reduce 方法
Array.prototype.customReduce = function (callback, initialValue) {
    if (this.length === 0 && initialValue === undefined) {
        throw new TypeError('Reduce of empty array with no initial value');
    }

    let accumulator = initialValue !== undefined ? initialValue : this[0];
    const startIndex = initialValue !== undefined ? 0 : 1;

    for (let i = startIndex; i < this.length; i++) {
        accumulator = callback(accumulator, this[i], i, this);
    }

    return accumulator;
};

// 示例
const numbers = [1, 2, 3, 4];

// 使用自定义 reduce 方法求和
const sum = numbers.customReduce((acc, curr) => acc + curr, 0);
console.log(sum); // 输出 10
